/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.start;

import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.RESessionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.connection.REConnection;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSessionBuilder;
import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.killHanlder.KillHandler;
import cn.ac.iie.di.wx.queueservice.SendToQueueThread;
import cn.ac.iie.di.wx.readFromFile.ReadFromFile;
import cn.ac.iie.di.wx.vo.PicUrlVo;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class start {

    static Logger logger = Logger.getLogger(start.class.getName());

    public static void main(String[] args) throws RESessionException, REConnectionException, InterruptedException {
        try {
            init();
            REConnection conn = new REConnection((String) RuntimeEnv.getParam(RuntimeEnv.RECONNECTION));

            //生成builder，参数为topic，由具体业务决定
            RESendSessionBuilder builder = (RESendSessionBuilder) conn.getSendSessionBuilder((String) RuntimeEnv.getParam(RuntimeEnv.SSBUILDER));
            //设置数据结构，参数为每一列的列名、列类型、是否可以为空。注意： 如果设置为不能为空，那么如果添加数据时没有添加该列数据，会报错。
//            builder.addColumn((String) RuntimeEnv.getParam(RuntimeEnv.ADDCOLUMN), REFieldType.String, true);
            builder.addColumn((String) RuntimeEnv.getParam(RuntimeEnv.ADDCOLUMN), REFieldType.Strings, true);
            //生成session
            LinkedBlockingQueue<PicUrlVo> picUrlBlockingQueue = new LinkedBlockingQueue<>(200);
            ReadFromFile readFromFile
                    = new ReadFromFile(
                            (String) RuntimeEnv.getParam(RuntimeEnv.RFFPATH),
                            (String) RuntimeEnv.getParam(RuntimeEnv.FNAME),
                            picUrlBlockingQueue);
            Thread readFromFileThread = new Thread(readFromFile);
            readFromFileThread.start();

            RESendSession session = (RESendSession) builder.build();
//        session.start();
            SendToQueueThread demo = new SendToQueueThread(session, (String) RuntimeEnv.getParam(RuntimeEnv.ADDCOLUMN), picUrlBlockingQueue);
            Thread t = new Thread(demo);
            t.start();
            //21日添加结束
            KillHandler killHandler = new KillHandler();
            killHandler.registerSignal("TERM");
            killHandler.setPicUrlBlockingQueue(picUrlBlockingQueue);
            killHandler.setSession(session);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

    public static void init() throws Exception {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        String configurationFileName = "cconfiguration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info(RuntimeEnv.getParam(RuntimeEnv.ADDCOLUMN));
            logger.info("init success");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

}
